<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html >
 <%@include file="./resource/head.jsp" %> 
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Werewolf</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

  </head>
  <body ng-app="app" ng-controller="game">
  	<nav class="navbar navbar-default">
	    <div class="navbar-header">
	      <a class="navbar-brand" href="/werewolf">
	        <img alt="狼人杀" src="">
	      </a>
	    </div>
	</nav>
	<div class="container">
	  	<page class="row active" id="init" ng-hide="success">
	  	<div class="alert alert-danger" role="alert" ng-if="!success">{{msg}}</div>
	  	<div class="col-xs-12" >
	  		<div class="form-horizontal">
	  			<label class="control-label">玩家</label>
	  			<div class="form-group">
					<label class="col-xs-2 control-label">号码牌</label>
					<div class="col-xs-10">
						<input type="number" class="form-control" placeholder="输入编号" ng-model="number">
					</div>
				</div>
	  			<div class="form-group">
					<label class="col-xs-2 control-label">昵称</label>
					<div class="col-xs-10">
						<input type="text" class="form-control" placeholder="输入姓名" ng-model="player">
					</div>
				</div>
	  			<div class="form-group">
					<label class="col-xs-2 control-label">密码</label>
					<div class="col-xs-10">
						<input type="password" class="form-control" placeholder="输入简单的密码..." ng-model="password">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-2 control-label">角色</label>
					<div class="col-xs-10">
						<select name="role" id="" class="form-control" ng-model="role">
							<option value="{{o.key}}" ng-repeat="o in rolelist">{{o.value}}</option>
						</select>
					</div>
				</div>
				<button ng-click="next();" class="btn btn-primary" ng-disabled="number==''||number==null||role==null||role==''">
					加入游戏
				</button>
	  		</div>
	  	</div>
	  	</page>
	  	<!-- 游戏界面 -->
	  	<page ng-show="success" class="row active">
	  		<h3>{{number}}号玩家</h3>
		  	<day ng-if="day">
				<row class="row">
					<div class="col-xs-12">
						<button class="btn btn-primary" ng-click="refresh()">刷新列表</button>
					</div>
				</row>
				<div class="alert alert-success" role="alert" ng-if="!firstDay">{{godword}}</div>
				<div class="alert alert-success" role="alert" ng-if="firstDay">
					<button class="btn btn-info" ng-click="getGodWord();">竞选警长完毕！获取上帝信息！</button>
				</div>
				<row class="row">
					<div class="col-xs-12">
						<button class="btn btn-primary" ng-click="night()">天黑请闭眼</button>
					</div>
				</row>
				<row class="row" ng-if="!firstDay">
					<div class="col-xs-12" >
					  	<label for="" class="col-xs-12">玩家列表</label>
					  	<div class="col-xs-6 col-lg-4" ng-repeat="one in playerList">
					  		<button class="{{one.out==true?'btn btn-danger':'btn btn-info'}}" ng-disabled="one.out==true" ng-click="vote(one);">
					  			{{one.number}}号玩家,{{one.name}}
					  		</button>
					  	</div>
					</div>
				</row>
				<row class="row" ng-if="firstDay">
					<div class="col-xs-12" >
					  	<label for="" class="col-xs-12">玩家列表</label>
					  	<div class="col-xs-6 col-lg-4" ng-repeat="one in playerList">
					  		<button class="btn btn-info">
					  			{{one.number}}号玩家,{{one.name}}
					  		</button>
					  	</div>
					</div>
				</row>
			</day>
			<night ng-if="!day">
		  		<citizen ng-include="'/werewolf/jsp/role/citizen.html'" ng-if="rolename=='村民'||rolename=='猎人'||rolename=='白痴'"></citizen>
		  		<wolf ng-include="'/werewolf/jsp/role/wolf.html'" ng-if="rolename=='狼人'"></wolf>
		  		<wolf ng-include="'/werewolf/jsp/role/witch.html'" ng-if="rolename=='女巫'"></wolf>
		  		<wolf ng-include="'/werewolf/jsp/role/forsee.html'" ng-if="rolename=='预言家'"></wolf>
		  		<wolf ng-include="'/werewolf/jsp/role/guard.html'" ng-if="rolename=='守卫'"></wolf>
	  		</night>
	  	</page>
  	</div>
  	<audio id="ring" src="/werewolf/static/wav/WindowsShutdown.MP3"></audio>
  </body>
  <%@include file="./resource/foot.jsp" %>  
  <script>
	$(document).on('touchstart', function() {
		$('#ring')[0].load()
	})
  var module=angular.module("app",[]);
  var gameList=module.controller("game",function($scope,$http,$interval,$timeout){
	  $scope.success=false;
	  $scope.rolelist=${rolelist};
	  $scope.game='${game}';
	  $scope.day=true;
	  //加入游戏
	  $scope.next=function(){
		  $http({
			  method:'post',
			  url:'/werewolf/player/input',
			  data:{
				  'number':$scope.number,
				  'name':$scope.player,
				  'role':$scope.role,
				  'password':$scope.password,
				  'game':$scope.game
			  }
		  }).success(function(data){
			  $scope.success=data.success;
			  $scope.msg=data.msg;
			  $scope.playerList=data.playerList;
			  $scope.rolename=data.rolename;
			  $scope.firstDay=data.firstDay;
			  $scope.godword=data.godword;
		  });
	  };
	  //进入黑夜
	  $scope.night=function(){
		  $.confirm({
			    title: '确认!',
			    content: '确定要进入黑夜吗?',
			    theme:'black',
			    confirmButton:'确认',
			    cancelButton:'取消',
			    confirm: function(){
			    	$http({
						  method:'post',
						  url:'/werewolf/player/night',
						  data:{
							  'game':$scope.game,
							  'number':$scope.number
						  }
					  }).success(function(data){
						  $.alert({
							    title: '注意!',
							    content: data.msg,
							    confirmButton: '我知道了',
							    theme:'black'
							});
						  if(data.success){
							  $scope.day=false;
							  $scope.done=false;
							  $scope.wolfdone=false;
							  $scope.playerList=data.playerList;
							  if($scope.tipRing==null&&$scope.number==1){
								  $scope.tipRing=$interval(function(){
									  finishedTips();
								  },2000);
							  }
						  }
					  })
			    }
			});
		  
	  }
	  //提示音
	  var finishedTips=function(){
		  $http({
			  method:'post',
			  url:'/werewolf/player/tips',
			  data:$scope.game
		  }).success(function(data){
			  var rand = Math.random()*5000;
			  if(data.wolfready&&!$scope.wolftips){
				  $timeout(function(){
					  $("#ring")[0].play();
				  },3000);
				  $scope.wolftips=true;
				  return;
			  }
			  if(data.forseeready&&!$scope.forseetips&&$scope.wolftips){
				  if(data.forseedead){
					  delay=10000+rand;
				  }else{
					  delay=3000;
				  }
				  $timeout(function(){
					  $("#ring")[0].play();
				  },delay);
				  $scope.forseetips=true;
				  return;
			  }
			  if(data.witchready&&!$scope.witchtips&&$scope.forseetips&&$scope.wolftips){
				  if(data.witchdead){
					  delay=10000+rand;
				  }else{
					  delay=3000;
				  }
				  $timeout(function(){
					  $("#ring")[0].play();
				  },delay);
				  $scope.witchtips=true;
				  return;
			  }
			  if(data.guardready&&!$scope.guardtips&&$scope.witchtips&&$scope.forseetips&&$scope.wolftips){
				  if(data.guarddead){
					  delay=10000+rand;
				  }else{
					  delay=3000;
				  }
				  $timeout(function(){
					  $("#ring")[0].play();
				  },delay);
				  $scope.guardtips=true;
				  return;
			  }
		  });
	  }
	  
	  //刷新
	  $scope.refresh=function(){
		  $http({
			  method:'post',
			  url:'/werewolf/player/refresh',
			  data:$scope.game
		  }).success(function(data){
			  $scope.playerList=data;
		  })
	  }
	  
	  //狼人杀人
	  $scope.kill=function(person){
		  var msg='你确定要杀'+person.number+'号'+person.name+'吗？';
		  if(person.number==null)msg="确定不杀人直接跳过吗？";
		  $.confirm({
			    title: '确认!',
			    content: msg,
			    theme:'black',
			    confirmButton:'确认',
			    cancelButton:'取消',
			    confirm: function(){
			    	$http({
						  method:'post',
						  url:'/werewolf/player/night',
						  data:{
							  'game':$scope.game,
							  'number':$scope.number
						  }
					  }).success(function(data){
						  $http({
							  method:'post',
							  url:'/werewolf/player/kill',
							  data:{
								  'person':person,
								  'game':$scope.game
							  }
						  }).success(function(data){
							  //提示信息
							  $.alert({
								    title: '注意!',
								    content: data.msg,
								    confirmButton: '我知道了',
								    theme:'black'
								});
							  $scope.done=data.done;
						  });
					  })
			    }
			    
			});
		  
		  
		  
	  }
	  //女巫开启刷新
	  $scope.wrefresh=function(){
		  witchrefresh();
		  if($scope.timeWitch!=null)return;
		  $scope.timeWitch=$interval(function(){
			  witchrefresh();
		  },2000);
	  }
	  //女巫刷新
	  var witchrefresh=function(){
		  $http({
			  method:'post',
			  url:'/werewolf/player/wrefresh',
			  data:$scope.game
		  }).success(function(data){
			  $scope.wolfdone=data.wolfdone;
			  $scope.personDead=data.personDead;
			  $scope.hasHeal=data.hasHeal;
			  $scope.hasPoison=data.hasPoison;
			  if($scope.wolfdone){
				  $interval.cancel($scope.timeWitch);
			      $scope.witchrefreshword="";
			      $scope.timeWitch=null;
			  }else{
			      $scope.witchrefreshword="狼人很墨迹，正在持续刷新...";
			  }
		  });
	  }
	//女巫救人
	  $scope.heal=function(heal){
		  $.confirm({
			    title: '确认!',
			    content: '确定要救他吗？',
			    theme:'black',
			    confirmButton:'确认',
			    cancelButton:'取消',
			    confirm: function(){
			    	$http({
						  method:'post',
						  url:'/werewolf/player/heal',
						  data:$scope.game
					  }).success(function(data){
						//提示信息
						  $.alert({
							    title: '注意!',
							    content: data.msg,
							    confirmButton: '我知道了',
							    theme:'black'
							});
						  $scope.done=data.done;
					  });
			    }
			    
			});
		  
	  }
	//女巫杀人
	$scope.witchkill=function(person){
		$.confirm({
		    title: '确认!',
		    content: '你确定要毒'+person.number+'号'+person.name+'吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
		    	$http({
					  method:'post',
					  url:'/werewolf/player/witchkill',
					  data:{
						  'person':person,
						  'game':$scope.game
					  }
				  }).success(function(data){
					  //提示信息
					  $.alert({
					    title: '注意!',
					    content: data.msg,
					    confirmButton: '我知道了',
					    theme:'black'
						});
					  $scope.done=data.done;
				  });
		    }
		    
		});
		  
	}
  //跳过女巫
  $scope.witchpass=function(){
	  $.confirm({
		    title: '确认!',
		    content: '确定什么都不做吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
		    	$http({
					  method:'post',
					  url:'/werewolf/player/witchpass',
					  data:$scope.game
				  }).success(function(data){
					  $scope.done=data.done;
				  });
		    }
		    
		});
		  
  }
  //预言家
  $scope.forsee=function(person){
	  $.confirm({
		    title: '确认!',
		    content: '你确定要验'+person.number+'号'+person.name+'吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
		    	$http({
					  method:'post',
					  url:'/werewolf/player/forsee',
					  data:{
						  'person':person,
						  'game':$scope.game
					  }
				  }).success(function(data){
					//提示信息
					  $.alert({
					    title: '注意!',
					    content: data.msg,
					    confirmButton: '我知道了',
					    theme:'black'
						});
					  $scope.done=data.done;
				  });
		    }
		    
		});
		  
  }
  //守卫
  $scope.guard=function(person){
	  $.confirm({
		    title: '确认!',
		    content: '你确定要守护'+person.number+'号'+person.name+'吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
		    	$http({
					  method:'post',
					  url:'/werewolf/player/forsee',
					  data:{
						  'person':person,
						  'game':$scope.game
					  }
				  }).success(function(data){
					  $http({
						  method:'post',
						  url:'/werewolf/player/guard',
						  data:{
							  'person':person,
							  'game':$scope.game
						  }
					  }).success(function(data){
						//提示信息
						  $.alert({
						    title: '注意!',
						    content: data.msg,
						    confirmButton: '我知道了',
						    theme:'black'
							});
						  $scope.done=data.done;
					  });
				  });
		    }
		    
		});
		  
  }
  //天亮了没？
  $scope.daylight=function(){
	  light();
	  if($scope.timeDay!=null)return;
	  $scope.timeDay=$interval(function(){
		  light();
	  },2000);
  }
  //天亮了
  var light=function(){
	  $http({
		  method:'post',
		  url:'/werewolf/player/day',
		  data:{
			  'game':$scope.game,
			  'number':$scope.number
		  }
	  }).success(function(data){
		  $scope.day=data.day;
		  $scope.godword=data.godword;
		  $scope.playerList=data.playerList;
		  if($scope.day){
		      $scope.refreshword="";
		      //关闭白天等待判断
			  $interval.cancel($scope.timeDay);
		      $scope.timeDay=null;
		      //关闭铃声提示
			  $interval.cancel($scope.tipRing);
			  $scope.tipRing=null;
			  //将是否提示置为否
			  $scope.wolftips=false;
			  $scope.forseetips=false;
			  $scope.witchtips=false;
			  $scope.guardtips=false;
		      $scope.firstDay=data.firstDay;
		  }else{
		      $scope.refreshword="有人好墨迹，正在持续刷新...";
		  }
	  });
  }
  //投票
  //1号玩家才可以投票
  $scope.vote=function(person){
	  if($scope.number!=1)return false;
	  $.confirm({
		    title: '确认!',
		    content: '你确定要放逐/开枪'+person.number+'号'+person.name+'吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
				  $http({
					  method:'post',
					  url:'/werewolf/player/vote',
					  data:{
						  'game':$scope.game,
						  'person':person
					  }
				  }).success(function(data){
					  if(data.msg!=null){
						//提示信息
						  $.alert({
						    title: '注意!',
						    content: data.msg,
						    confirmButton: '我知道了',
						    theme:'black'
							});
					  }
					  $scope.playerList=data.playerList;
				  });
		    }
		    
		});
	  
  }
  //跳过守卫
  $scope.guardpass=function(){
	  $.confirm({
		    title: '确认!',
		    content: '确定什么都不做吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
			    	$http({
						  method:'post',
						  url:'/werewolf/player/guardpass',
						  data:$scope.game
					  }).success(function(data){
						  $scope.done=data.done;
					  });
		    }
	  });
  }
  //获取上帝信息
  $scope.getGodWord=function(){
	  $.confirm({
		    title: '确认!',
		    content: '警长精选完毕了吗？',
		    theme:'black',
		    confirmButton:'确认',
		    cancelButton:'取消',
		    confirm: function(){
				$scope.firstDay=false;
				$scope.$apply();
		    }
	  });
  }
});
  </script>
</html>
